/**  
  -- The Performance Driven Lime Project -- 

  A self adaptive tuple space for mobile agents and hosts by dynamic
  adapting tuple's replication policy.

  Copyright (C) 2008
  Xing Jiankuan, Pan Wenhui
  SAGroup of Tsinghua University

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  Lesser General Public License for more details.

  You should have received this copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
 */
package lime2.ca.treeplain;

import java.util.ArrayList;
import java.util.HashSet;

import lime2.LimeCommunicationException;
import lime2.LimeServerID;
import lime2.RemTupleOp;
import lime2.RemoteOp;
import lime2.ca.CastInfo;
import lime2.ca.ICommunicationAdapter;
import lime2.ca.treeplain.CastTree;

/**
 * @author Xing Jiankuan
 *
 */
public class TreeCastInfo implements CastInfo {

	/**
	 * 
	 */
	private static final long serialVersionUID = -2731077073303230738L;

	private CastTree castTree;
	
	/**
	 * Current vertex that marks Location.HERE
	 */
	private Vertex curVertex; 

	public TreeCastInfo(CastTree tree) {
		castTree = tree;
		curVertex = castTree.root;
	}

	public boolean isEmpty() {
		if (castTree == null || castTree.root.children == null || castTree.root.children.isEmpty())
			return true;
		else
			return false;
	}

	public LimeServerID getCurrentServer() {
		return curVertex.server;
	}
	
	public HashSet<LimeServerID> performMulticast(ICommunicationAdapter ca, RemoteOp ro) throws LimeCommunicationException {
		HashSet<LimeServerID> ret = new HashSet<LimeServerID>();
		if (curVertex != null) {
//			System.out.println(this.toString());
			Vertex here = curVertex; // since this.curVertex equals to dInfo.curVertex, we just store it and restore it later
			ArrayList<Vertex> subVerts = curVertex.children;
			if(subVerts != null) {
				TreeCastInfo dInfo = (TreeCastInfo)ro.getCastInfo();
				
				for (Vertex subVert: subVerts) {
					dInfo.curVertex = subVert;
					//set the correct destination
					if(ro instanceof RemTupleOp) {
						RemTupleOp rto = (RemTupleOp) ro;
						rto.setDest(subVert.server);
					}
					ca.sendOp(subVert.server, ro);
					ret.add(subVert.server);
				}
				if(ro instanceof RemTupleOp) {
					((RemTupleOp)ro).setDest(here.server); //restore the correct destination
				}
			}
			curVertex = here;
		}
		return ret;
	}
	
	public String toString() {
		if(castTree != null) {
			return "cur:" + curVertex.server + "\nTree:\n" + castTree.toString();
			
		} else {
			return "NULL_CAST_TREE";
		}
	}

	@Override
	public LimeServerID[] getDirectCastTargets() {
		if(castTree == null || curVertex == null || curVertex.children == null) {
			return new LimeServerID[0];
		} else {
			LimeServerID[] ret = new LimeServerID[curVertex.children.size()];
			for(int i = 0; i < ret.length; i++) {
				ret[i] = curVertex.children.get(i).server;
			}
			return ret;
		}
	}
}
